﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MinhUtils
{
    public static class Common
    {
        public static bool IsNull(object obj)
        {
            if (obj == DBNull.Value) return true;
            if (obj == null) return true;
            return false;
        }

        /// <summary>
        /// Return the percentage in integer between 2 given numbers. E.g (2,3) will returns 67
        /// </summary>
        public static int GetPercentage(double a, double b)
        {
            return (int)Math.Round(a / b * 100, 0);
        }

        /// <summary>
        /// Return the percentage in double between 2 given numbers. E.g (2,3) will returns 66.67
        /// </summary>
        public static double GetPercentageDouble(double a, double b)
        {
            return Math.Round(a / b * 100, 2);
        }

        /// <summary>
        /// Return the percentage in string between 2 given numbers. E.g (2,3) will returns a string "67%"
        /// </summary>
        public static string GetPercentageString(double a, double b)
        {
            return GetPercentage(a, b) + "%";
        }

        /// <summary>
        /// Return the percentage in string between 4 given numbers. E.g (1, 2, 1, 2) will returns a string "25%". Hard to explain I know. It confuses me sometimes too.
        /// </summary>
        public static string GetPercentageString(double a, double b, double c, double d)
        {
            return Math.Round((a - 1) / b * 100 + c / d / b * 100, 0) + "%";
        }

        public static bool IsStoppedOrAborted(System.Threading.Thread thread)
        {
            if (thread == null) return true;

            return (thread.ThreadState == System.Threading.ThreadState.Stopped || thread.ThreadState == System.Threading.ThreadState.Aborted);
        }
    }
}
